﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using SyslogServer;
using Microsoft.Win32;

namespace SyslogServerConfig
{
    public partial class Form1 : Form
    {
        private const string regRoot = @"HKEY_LOCAL_MACHINE\SOFTWARE\SysLogServer";
        private enum logLevels
        {
            none,
            exception,
            error,
            warning,
            info,
            debug
        }
        public int portNumber;
        private logLevels logLevel;
        private string logSource;
        private string logto;
        public IPAddress listenOn;
        private bool protoTCP;
        private bool protoUDP;
        private bool toEventlog;
        private bool toSQLDB;
        private string sqlServer;
        private string sqlDatabase;

        public Form1()
        {
            InitializeComponent();
            try
            {
                Registry.LocalMachine.CreateSubKey("\\SOFTWARE\\SysLogServer");
            }
            catch
            {
            }
            portNumber = (int)Registry.GetValue(regRoot, "PortNumber", 514);
            logLevel = (logLevels)Registry.GetValue(regRoot, "LogLevel", logLevels.error);
            logSource = (string)Registry.GetValue(regRoot, "Log Source", "Syslog Server");
            logto = (string)Registry.GetValue(regRoot, "Log Name", "Syslog Server");
            listenOn = IPAddress.Any;
            IPAddress.TryParse((string)Registry.GetValue(regRoot, "Listenning IP", "0.0.0.0"), out listenOn);
            protoTCP = (((string)Registry.GetValue(regRoot, "Protocol", "TCP,UDP")).ToLower().Contains("tcp"));
            protoUDP = (((string)Registry.GetValue(regRoot, "Protocol", "TCP,UDP")).ToLower().Contains("udp"));
            toEventlog = (((string)Registry.GetValue(regRoot, "Store in Eventlog", "No")).ToLower().Contains("yes"));
            toSQLDB = (((string)Registry.GetValue(regRoot, "Store in SQL Database", "Yes")).ToLower().Contains("yes"));
            sqlServer = (string)Registry.GetValue(regRoot, "SQL Server", "localhost");
            sqlDatabase = (string)Registry.GetValue(regRoot, "SQL Database", "SysLogDB");
            ipbox.Text = listenOn.ToString();
            portbox.Text = portNumber.ToString();
            loglevelcombo.SelectedIndex = (int)logLevel;
            sourcebox.Text = logSource;
            logbox.Text = logto;
            tcpchk.Checked = protoTCP;
            udpchk.Checked = protoUDP;
            eventchk.Checked = toEventlog;
            sqlchk.Checked = toSQLDB;
            sqlsvrbox.Text = sqlServer;
            sqldbbox.Text = sqlDatabase;
        }



        private void button1_Click(object sender, EventArgs e)
        {
            SyslogServer.SyslogMessage mysyslogMsg = new SyslogServer.SyslogMessage(msgtoconvert.Text);
            string s = "";
            s += "Header:\r\n" + mysyslogMsg.Header + "\r\n\r\n";
            s += "Structured Data:\r\n" + mysyslogMsg.SD + "\r\n\r\n";
            s += "Message:\r\n" + mysyslogMsg.Msg + "\r\n\r\n";
            rstBox.Text = s;

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            listenOn = IPAddress.Parse(ipbox.Text);
            portNumber=Int16.Parse( portbox.Text);
            logLevel= (logLevels)loglevelcombo.SelectedIndex ;
            logSource= sourcebox.Text;
            logto= logbox.Text;
            protoTCP= tcpchk.Checked ;
            protoUDP= udpchk.Checked ;
            toEventlog= eventchk.Checked ;
            toSQLDB= sqlchk.Checked ;
            sqlServer= sqlsvrbox.Text ;
            sqlDatabase= sqldbbox.Text ;
            Registry.SetValue(regRoot, "PortNumber", portNumber);
            Registry.SetValue(regRoot, "LogLevel", (int)logLevel);
            Registry.SetValue(regRoot, "Log Source", logSource);
            Registry.SetValue(regRoot, "Log Name", logto);
            Registry.SetValue(regRoot, "Listenning IP", listenOn.ToString());
            if (protoTCP & protoUDP) Registry.SetValue(regRoot, "Protocol", "TCP,UDP");
            else
                if (protoTCP) Registry.SetValue(regRoot, "Protocol", "TCP");
                else
                    if (protoUDP) Registry.SetValue(regRoot, "Protocol", "UDP");
                    else
                        Registry.SetValue(regRoot, "Protocol", "None");
            if (toEventlog) Registry.SetValue(regRoot, "Store in Eventlog", "Yes");
            else
                Registry.SetValue(regRoot, "Store in Eventlog", "No");
            if (toSQLDB) Registry.SetValue(regRoot, "Store in SQL Database", "Yes");
            else
                Registry.SetValue(regRoot, "Store in SQL Database", "No");
            Registry.SetValue(regRoot, "SQL Server", sqlServer);
            Registry.SetValue(regRoot, "SQL Database", sqlDatabase);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            testresult.Text = SyslogMessage.IsStructuredMessage(textBox1.Text).ToString();
            testresult.Visible = true;
            if (testresult.Text.ToLower().Substring(0,1)=="t")
            {
                SyslogMessage mymsg = new SyslogMessage(textBox1.Text);
                label10.Visible = true;
                label11.Visible = true;
                label12.Visible = true;
                label10.Text = mymsg.Header;
                label11.Text = mymsg.SD;
                label12.Text = mymsg.Msg;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            testresult.Visible = false;
            label10.Visible = false;
            label11.Visible = false;
            label12.Visible = false;
        }
    }



}
